<?php

include "validate.php";
include "db_con.php";

if (isset($_GET['sensor_name']))
{
	$input = $_GET['sensor_name'];
	if (strlen($input)>0)
	{
		$input = mysql_real_escape_string($input);
		
		$query = "	SELECT name AS name, region AS region, address AS address, comments AS comments 
					FROM sensor 
					WHERE name = '" . $input . "';";
		$result = mysql_query($query);
		echo "<?xml version='1.0'?><data>";
		if (mysql_num_rows($result)==0)
		{
			echo "<element>ERROR</element>";
		}
		else
		{
			$row = mysql_fetch_array($result);
			echo "<element>" . htmlspecialchars($row['name']) . "</element>
					<element>" . htmlspecialchars($row['region']) . "</element>
					<element>" . htmlspecialchars($row['address']) . "</element>
					<element>" . htmlspecialchars($row['comments']) . "</element>";
			
			$query = "	SELECT update_time, is_active  
						FROM records 
						INNER JOIN sensor ON sensor.sensor_id = records.sensor_id 
						WHERE sensor.name = '" . $input . "';";
			
			$result = mysql_query($query);
			
			$counter = 0;
			$output = "";
			while ($row = mysql_fetch_array($result))
			{
				$output = $output . "<element>" . $row['update_time'] . "</element>";
				$a[] = $row['is_active'];
				$counter++;
			}
			
			echo "<element>" . $counter . "</element>" . $output;
			for ($i=0; $i<count($a); $i++)
			{
				echo "<element>" . $a[$i] . "</element>";
			}
		}
		echo "</data>";
	}
}
?>
